package cn.itcast.core.service.impl;

import cn.itcast.core.bean.TestCase;
import cn.itcast.core.dao.TestCaseMapper;
import cn.itcast.core.service.TestCaseService;
import cn.itcast.core.utils.Page;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TestCaseServiceImpl implements TestCaseService {
    private static final Logger log = LoggerFactory.getLogger(TestCaseServiceImpl.class);

    @Autowired
    TestCaseMapper testCaseMapper;
     public TestCase findCaseListById(Long id){
         TestCase testCase= testCaseMapper.selectByPrimaryKey(id);
         log.info("日志  {}", JSON.toJSONString(testCase));
         return  testCase;
     }
    public int insertinsertSelectiveCase(TestCase testCase){
        testCase.setId(testCase.getId());
        if (StringUtils.isNotBlank(testCase.getCaseName())) {
            testCase.setCaseName(testCase.getCaseName());
        }
        if (StringUtils.isNotBlank(testCase.getInterfaceName())) {
            testCase.setCaseName(testCase.getInterfaceName());
        }
        if (StringUtils.isNotBlank(testCase.getUrl())) {
            testCase.setCaseName(testCase.getUrl());
        }
        if (StringUtils.isNotBlank(testCase.getInterfaceType())) {
            testCase.setCaseName(testCase.getInterfaceType());
        }
        if (StringUtils.isNotBlank(testCase.getData())) {
            testCase.setCaseName(testCase.getData());
        }
        if (StringUtils.isNotBlank(testCase.getHeader())) {
            testCase.setCaseName(testCase.getHeader());
        }
        if (StringUtils.isNotBlank(testCase.getInterfaceCookie())) {
            testCase.setCaseName(testCase.getInterfaceCookie());
        }
        if (testCase.getInterfaceDependId()!=0){
            testCase.setInterfaceDependId(testCase.getInterfaceDependId());
        }
        if (StringUtils.isNotBlank(testCase.getInterfaceDependData())) {
            testCase.setCaseName(testCase.getInterfaceDependData());
        }
        if (StringUtils.isNotBlank(testCase.getResponse())) {
            testCase.setCaseName(testCase.getResponse());
        }
        if (StringUtils.isNotBlank(testCase.getExpectedResult())) {
            testCase.setCaseName(testCase.getExpectedResult());
        }
        if (StringUtils.isNotBlank(testCase.getTextResult())) {
            testCase.setCaseName(testCase.getTextResult());
        }
        int res=testCaseMapper.insertSelective(testCase);
        return res;
    }
    public int  deleteCase(Long id){

        int res=testCaseMapper.deleteByPrimaryKey(id);
        return res;
    }
    public  int updateCase(TestCase testCase){
        testCase.setId(testCase.getId());
        if (StringUtils.isNotBlank(testCase.getCaseName())) {
            testCase.setCaseName(testCase.getCaseName());
        }
        if (StringUtils.isNotBlank(testCase.getInterfaceName())) {
            testCase.setCaseName(testCase.getInterfaceName());
        }
        if (StringUtils.isNotBlank(testCase.getUrl())) {
            testCase.setCaseName(testCase.getUrl());
        }
        if (StringUtils.isNotBlank(testCase.getInterfaceType())) {
            testCase.setCaseName(testCase.getInterfaceType());
        }
        if (StringUtils.isNotBlank(testCase.getData())) {
            testCase.setCaseName(testCase.getData());
        }
        if (StringUtils.isNotBlank(testCase.getHeader())) {
            testCase.setCaseName(testCase.getHeader());
        }
        if (StringUtils.isNotBlank(testCase.getInterfaceCookie())) {
            testCase.setCaseName(testCase.getInterfaceCookie());
        }
        if (testCase.getInterfaceDependId()!=0){
            testCase.setInterfaceDependId(testCase.getInterfaceDependId());
        }
        if (StringUtils.isNotBlank(testCase.getInterfaceDependData())) {
            testCase.setCaseName(testCase.getInterfaceDependData());
        }
        if (StringUtils.isNotBlank(testCase.getResponse())) {
            testCase.setCaseName(testCase.getResponse());
        }
        if (StringUtils.isNotBlank(testCase.getExpectedResult())) {
            testCase.setCaseName(testCase.getExpectedResult());
        }
        if (StringUtils.isNotBlank(testCase.getTextResult())) {
            testCase.setCaseName(testCase.getTextResult());
        }
        int res=testCaseMapper.updateByPrimaryKeySelective(testCase);
        return  res;
    }
    public List<TestCase> selectByTestCase(String interfaceName,String interfaceType,String textResult){
       List <TestCase> testCaselist=testCaseMapper.selectByTestcase(interfaceName,interfaceType,textResult);
        return  null;
    }


    public int selectTestListCount( String interfaceName, String interfaceType, String textResult){
        int count=testCaseMapper.selectTestListCount(interfaceName,interfaceType,textResult);
        return  count;
    }
    public Page<TestCase> paging(String interfaceName, String interfaceType, String textResult, int currPage, int pageSize){
        Page  page=new Page();
        //startRows 是limit的开始位置
        int startRow=(currPage-1)* pageSize;
        //查出的是分页后的结果，如果查询的count大于10条 那么第一页的testCaseList应该是10条
        List<TestCase> testCaseList=testCaseMapper.selectTestCaseList(startRow,pageSize,interfaceName,interfaceType,textResult);
        page.setRows(testCaseList);
        page.setPage(startRow);
        page.setSize(pageSize);
        int count=testCaseMapper.selectTestListCount(interfaceName,interfaceType,textResult);
        page.setTotal(count);
        return  page;

    };


}
